package com.lgx.mall.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lgx.common.entity.IntraCity;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.entity.Brand;
import com.lgx.mall.core.entity.Product;
import com.lgx.mall.core.service.IBrandService;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.vo.ProductVO;
import com.lgx.mall.view.service.IWebPageDetailService;

/**
 * 搜索
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private IWebPageDetailService webPageDetailService;
	@Autowired
	private IProductService productService;

	public IWebPageDetailService getWebPageDetailService() {
		return webPageDetailService;
	}

	public void setWebPageDetailService(IWebPageDetailService webPageDetailService) {
		this.webPageDetailService = webPageDetailService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	/**
	 * 主搜索方法
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		//搜索类型，0：商品，1：品牌
		String type = request.getParameter("type");	
		//搜索内容，商品名称，编号；品牌名称；模糊查询
		String content = request.getParameter("content");
		
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		
		String time = request.getParameter("time");
		if(StringUtils.isEmpty(time))time = "";
		String price = request.getParameter("price");
		if(StringUtils.isEmpty(price))price = "";
		
		if(type.equals("0")) {
			Pagination page = this.productService.findProductByNameOrCode(content, time, price, 20, Integer.parseInt(pageNo));
			request.setAttribute("page", page);
		} else if(type.equals("1")) {
			Pagination page = this.productService.findProductByBrandName(content, time, price, 20, Integer.parseInt(pageNo));
			request.setAttribute("page", page);
		}
		request.setAttribute("content", content);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("types", type);
		request.setAttribute("time", time);
		request.setAttribute("price", price);
		request.setAttribute("path", "search/index");
		return "search/index";
	}
	
	/**
	 * 商品分类类型搜索
	 * 
	 * @param categoryId 类型ID
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/c/{categoryId}.htm",method=RequestMethod.GET)
	public String searchCategory(@PathVariable("categoryId") long categoryId,HttpServletRequest request,HttpServletResponse response){
		//获取同城
		IntraCity city = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		String time = request.getParameter("time");
		if(StringUtils.isEmpty(time))time = "";
		String price = request.getParameter("price");
		if(StringUtils.isEmpty(price))price = "";
		
		
		Pagination page = this.productService.findProductByCategory(city==null?null:city.getAreaId(), 0, categoryId, 0,time,price,20, Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		request.setAttribute("categoryId", categoryId);
		String type = request.getParameter("type");	
		if(StringUtils.isEmpty(type))type = "0";
		request.setAttribute("types", type);
		request.setAttribute("time", time);
		request.setAttribute("price", price);
		request.setAttribute("path", "search/c/"+categoryId);
		return "search/index";
	}
	
	/**
	 * 
	 * @param areaCategory 导航区域
	 * @param categoryId 商品分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/c/{areaCategory}/{categoryId}.htm",method=RequestMethod.GET)
	public String searchAreaCategory(@PathVariable("areaCategory") long areaCategory,@PathVariable("categoryId") long categoryId,HttpServletRequest request,HttpServletResponse response){
		//获取同城
		IntraCity city = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		Pagination page = this.productService.findProductByCategory(city==null?null:city.getAreaId(), areaCategory, categoryId, 0,null,null,20,Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		return "search/index";
	}
	
	/**
	 * 商品品牌搜索
	 * 
	 * @param brandId 品牌ID
	 * @param request
	 * @param response     
	 * @return
	 */
	@RequestMapping(value="/b/{brandId}.htm",method=RequestMethod.GET)
	public String searchBrand(@PathVariable("brandId") long brandId,HttpServletRequest request,HttpServletResponse response){
		//获取同城
		IntraCity city = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		Pagination page = this.productService.findProductByCategory(city==null?null:city.getAreaId(), 0, 0, brandId,null,null,20,Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		return "search/index";
	}
	
	/**
	 * 商品品牌搜索
	 * 
	 * @param brandId 品牌ID
	 * @param request
	 * @param response     
	 * @return
	 */
	@RequestMapping(value="/b/{areaCategory}/{brandId}.htm",method=RequestMethod.GET)
	public String searchAreaBrand(@PathVariable("areaCategory") long areaCategory,@PathVariable("brandId") long brandId,HttpServletRequest request,HttpServletResponse response){
		//获取同城
		IntraCity city = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		Pagination page = this.productService.findProductByCategory(city==null?null:city.getAreaId(), areaCategory, 0, brandId,null,null,20,Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		return "search/index";
	}
	
	@RequestMapping("/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "search/head";
	}

	@RequestMapping("/search.ftl")
	public String search(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//上周热兑换榜
		long categoryId = 133;
		List<ProductVO> details = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		request.setAttribute("details", details);

		//猜你喜欢的
		List<Product> guessproducts = this.productService.guessYouLike(5);
		request.setAttribute("guessproducts", guessproducts);
		
		return "search/search";
	}
}
